<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF - 8">
    <meta name="viewport" content="width=device - width, initial - scale = 1.0">
    <title>Layui数据实时更新(方法一)(存在问题：res.code = undifined报错)</title>
    <link rel="stylesheet" href="/app/admin/component/layui/css/layui.css?v=2.8.12">
</head>
<body>
<table id="dataTable" lay - filter="dataTableFilter"></table>
<script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
<script>
    layui.use(['table', 'layer'], function () {
        var table = layui.table;
        var layer = layui.layer;
        // 渲染表格
        var renderTable = function () {
            fetch('http://127.0.0.1:8787/api/world')
                .then(response => response.json())
                .then(originalData => {
                    // 数据转换函数
                    function convertData(originalData) {
                        return {
                            code: 0,
                            msg: "成功",
                            data: [
                                {
                                    world_tree: originalData.world_tree
                                }
                            ]
                        };
                    }
                    var convertedData = convertData(originalData);

                    // 渲染表格
                    table.render({
                        elem: '#dataTable',
                        data: convertedData.data,
                        cols: [[
                            { field: 'world_tree', title: 'World Tree' }
                        ]],
                    });
                })
                .catch(error => {
                    console.error('请求错误:', error);
                    layer.msg('请求数据时发生错误');
                });
        };
        renderTable();
        // 每隔5秒（5000毫秒）重新渲染表格
        setInterval(renderTable, 5);
    });
</script>
</body>
</html>